import { requests } from '../utils/requests'
import { token } from '../config'

const item_v2 = {
  //通用GET请求 带token
  tokenGet: function (url, data = {}) {
    return httpFn(url, data)
  },
  //通用POST请求 带token
  tokenPost: function (url, data = {}) {
    return httpFn(url, data, 'POST')
  },
  //通用PUT请求 带token
  tokenPut: function (url, data = {}) {
    return httpFn(url, data, 'PUT')
  }
}

function httpFn(url, data, method = 'GET') {
  return new Promise((resolve, reject) => {
    requests({
      url,
      method,
      params: method === 'GET' ? data : '',
      data: method === 'POST' || 'PUT' ? data : '',
      headers: {
        'Authorization': 'Bearer ' + token
      }
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

export {
  item_v2
}